home *** CD-ROM | disk | FTP | other *** search
- /* e_stat.c */
-
- #include <Files.h>
- #include <Aliases.h>
- #include <errno.h>
- #include "sys/unistd.h"
- #include "sys/stat.h"
-
- #if defined(__MWERKS__) && (__MWERKS__ < 700)
- #define ioACUser filler2
- #endif
-
- int e_stat_guts( const char *path, struct stat *buf, int linkp )
- {
- FSSpec fss;
- OSErr err;
- CInfoPBRec cb;
- ParamBlockRec pb;
- Boolean isFolder, wasAliased;
- Str255 fnbuf;
- long cur_dir;
- short cur_vol;
- Str63 vname;
-
- c_to_p( path, fnbuf );
- HGetVol( NULL, &cur_vol, &cur_dir );
- // added to deal with aliases...
- err = FSMakeFSSpec( cur_vol, cur_dir, fnbuf, &fss );
- if( err != noErr
- || (linkp
- && (ResolveAliasFile( &fss, 1, &isFolder, &wasAliased ) != noErr)))
- { errno = ENOENT; return -1; }
- // get catalog info
- cb.hFileInfo.ioCompletion = nil;
- cb.hFileInfo.ioVRefNum = fss.vRefNum;
- cb.hFileInfo.ioDirID = fss.parID;
- cb.hFileInfo.ioFDirIndex = 0;
- cb.hFileInfo.ioNamePtr = fss.name;
- if( PBGetCatInfoSync(&cb) != 0 )
- { errno = ENOENT; return -1; }
- // get volume info
- pb.volumeParam.ioVolIndex = 0;
- pb.volumeParam.ioVRefNum = cb.hFileInfo.ioVRefNum;
- pb.volumeParam.ioNamePtr = vname;
- if( PBGetVInfoSync(&pb) != 0 )
- { errno = ENOENT; return -1; }
- if( buf == NULL ) return 0;
- // make the stat buf
- buf->st_dev = pb.ioParam.ioVRefNum;
- buf->st_ino = cb.dirInfo.ioDrDirID;
- buf->st_nlink = 1;
- buf->st_uid = getuid();
- buf->st_gid = getgid();
- buf->st_rdev = 0;
- #if ( __MWERKS__ >= 0x1100 )
- /* seconds between 1/1/1900 and 1/1/1904 */
- buf->st_atime = cb.hFileInfo.ioFlMdDat + (365L * 4L) * 24L * 60L * 60L;
- buf->st_mtime = cb.hFileInfo.ioFlMdDat + (365L * 4L) * 24L * 60L * 60L;
- buf->st_ctime = cb.hFileInfo.ioFlCrDat + (365L * 4L) * 24L * 60L * 60L;
- #else
- buf->st_atime = cb.hFileInfo.ioFlMdDat;
- buf->st_mtime = cb.hFileInfo.ioFlMdDat;
- buf->st_ctime = cb.hFileInfo.ioFlCrDat;
- #endif
- buf->st_blksize = pb.volumeParam.ioVAlBlkSiz;
- if( cb.dirInfo.ioFlAttrib & 0x10 )
- { // directory
- buf->st_mode = S_IFDIR | 0777;
- //if( cb.dirInfo.ioACUser & 0x04 ) /* locked */
- //if( cb.dirInfo.ioACUser & 0x01 ) /* no "see folders" priv */
- //if( cb.dirInfo.ioACUser & 0x02 ) /* no "see files" priv */
- buf->st_nlink = 2 + cb.dirInfo.ioDrNmFls;
- buf->st_size = cb.dirInfo.ioDrNmFls;
- }
- else
- { // file
- buf->st_nlink = 1;
- if (cb.hFileInfo.ioFlFndrInfo.fdFlags & 0x8000)
- {
- buf->st_mode = S_IFLNK | 0777;
- buf->st_size = buf->st_blksize;
- }
- else
- {
- buf->st_mode = S_IFREG | 0666;
- if( cb.hFileInfo.ioFlAttrib & 0x01 ) // locked
- buf->st_mode &= ~0222; // not writable
- switch (cb.hFileInfo.ioFlFndrInfo.fdType)
- { case 'APPL':
- case 'BINA':
- buf->st_mode |= 0111; // executable
- break;
- default:
- break;
- }
- buf->st_size = cb.hFileInfo.ioFlLgLen; // data fork only
- }
- }
- buf->st_blocks = (buf->st_size + buf->st_blksize - 1) / buf->st_blksize;
- return 0; /* All is well */
- }
-
- int e_stat(const char *path, struct stat *buf)
- {
- if (path)
- return (e_stat_guts( path, buf, 1 ));
- return (-1);
- }
-
- int e_lstat(const char *path, struct stat *buf)
- {
- if (path)
- return (e_stat_guts( path, buf, 0 ));
- return (-1);
- }
-
- // end
-